home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / grsear20.zip / GRINITUN.PAS < prev   
Pascal/Delphi Source File  |  1993-01-04  |  14KB  |  404 lines

  1. unit grinitun;
  2. { Author:
  3.     7/17/88    Michael Shunfenthal  Compuserve ID [76320,122]
  4.  
  5. PROGRAM FUNCTION
  6. Determine the adapter type, and search for the driver file.  For more
  7. info, refer to the file GRINIT.DOC.
  8. }
  9.  
  10. interface
  11. uses dos, crt, graph;
  12.  
  13. type debugrange = 0..2;
  14.     function grsearch
  15.           (environvar : string; var GraphDriver : integer;
  16.           var dirstring : string; grdebug : debugrange) : boolean;
  17.  
  18. {    Explanation:
  19.     function true: driver found and adapter initialized
  20.              false: an error prevents graphics initialization
  21.     environvar:  contains the desired environment variable name to be
  22.                  examined for the list of directories in the format of the
  23.                  path variable.  if null, ('') PATH is the default.
  24.     GraphDriver: graphics driver found by DetectGraph
  25.     dirstring:   contains the path to the required driver file
  26.     grdebug: 0 = no display.  use only function value to indicate status
  27.              1 = display errors only
  28.              2 = verbose: display errors, list the environment and 
  29.                   the directories in the specified environment variable
  30.  }
  31. implementation
  32.  
  33. function grsearch;
  34.  
  35. const
  36.    { max number of directories in the path }
  37.       MaxDirectories = 20;
  38.    { max length of each directory string }
  39.       MaxDirLength = 64;
  40.    { max space searched for environment }
  41.       MaxEnvironSpace = 32000;
  42.    { maximum length of the environment variable }
  43.       MaxVarLength = 1000;
  44.  
  45. type
  46.    bytepatharray = Array [1..MaxDirectories, 1..MaxDirLength] of byte;
  47.    maxdirtype = 0..MaxDirectories;
  48.  
  49. var
  50.    GraphMode   : integer;
  51.    graphstatus              : integer;
  52.    bgifile                  : string [8];
  53.    varfound                 : boolean;
  54.  
  55. procedure GraphicsDetermine; 
  56. { detect and return the adapter type, then
  57.    define the bgi file to be found }
  58.  
  59. begin
  60.    DetectGraph(GraphDriver, GraphMode );
  61.    case GraphDriver of          { set the BGI file to be found }
  62.       Reserved,
  63.       CGA      : bgifile := 'CGA';
  64.       EGA, EGA64, EGAMono,
  65.       MCGA, VGA: bgifile := 'EGAVGA';
  66.       Hercmono : bgifile := 'HERC';
  67.       ATT400   : bgifile := 'ATT';
  68.       PC3270   : bgifile := 'PC3270';
  69.    end;
  70.    graphstatus := GraphResult;
  71.    if graphstatus <> grOk
  72.       then      { test result of graphics operation }
  73.          begin
  74.             if (grdebug=1) or (grdebug=2)
  75.                then
  76.                writeln('DetectGraph error: ', GraphErrorMsg(GraphDriver));
  77.             Halt(1);
  78.          end;
  79.  end; { GraphicsDetermine }
  80.  
  81.  procedure SearchEnvironment ( var dircount : maxdirtype;
  82.                               var dirlist : bytepatharray );
  83.  { read the environment and extract the directory list }
  84.  var
  85.      Segment        : Integer;        { the two parts of an address }
  86.      offset,                          { index into environment space }
  87.      { offset where the variable begins }
  88.      offsetvarstart : 0 .. MaxEnvironSpace;
  89.  
  90. procedure ReadEnvironment;
  91. { read the environment area, searching for variables delimited by a null }
  92.  
  93. function locatevariable : boolean;
  94. { search for the specified variable, or 'PATH'}
  95.  
  96. label 1000, 2000;
  97.  
  98. var
  99.    index : integer;
  100.  
  101. begin
  102.  {prepare passed parameter for use: substitute 'PATH' if null, or
  103.  trim trailing spaces and convert to uppercase }
  104. if length( environvar)>0
  105.    then
  106.       for index := 1 to length (environvar) do
  107.          if environvar[index]=' '
  108.             then
  109.                begin
  110.                   environvar := copy(environvar,1,length(environvar)-1);
  111.                   goto 1000
  112.                end
  113.             else
  114.                environvar[index] := upcase( environvar[index])
  115.    else
  116.       environvar := 'PATH';
  117. { compare each character in the passed variable to the character
  118. in the environment }
  119. 1000: for index := 1 to length (environvar) do
  120.    if Mem[Segment:offset-1+index] <> ord( environvar[index])
  121.          then goto 2000;
  122.    { mark one more than the first character after the variable to
  123.    skip over the '=' sign }
  124.    offsetvarstart:=offset + length (environvar) +1;
  125.    locatevariable := true;
  126.    exit;
  127.    { mismatch: error exit }
  128.    2000: locatevariable := false;
  129. end; { locatevariable }
  130.  
  131.  
  132. Begin { ReadEnvironment }
  133.    offset := 0;              { set initial offsets }
  134.    if grdebug=2
  135.       then
  136.          begin
  137.             ClrScr;
  138.             writeln('The environment variables: ')
  139.          end;
  140.    While (offset < MaxEnvironSpace) do
  141.       begin
  142.          { call locatevariable to see if it is the first variable
  143.          in the environment }
  144.          if offset = 0
  145.             then
  146.                varfound := locatevariable;
  147.          if Mem[Segment:offset] = 0
  148.             then
  149.                begin
  150.                   if Mem[Segment:offset+1] = 0
  151.                      then
  152.                         begin
  153.                         { two nulls in a row indicate the end of the 
  154.                         environment }
  155.                            if grdebug=2
  156.                               then
  157.                                begin
  158.                                 writeln;
  159.                                 writeln('The DOS environment is ',offset,
  160.                                   ' bytes long.', environvar,
  161.                                   ' located at offset: ', offsetvarstart)
  162.                                end;
  163.                            exit
  164.                         end
  165.                      else
  166.                         { a single null indicates the end of one variable,
  167.                         so the call to locatevariable will not find one
  168.                         as part of another call only if the variable has
  169.                         not already been found}
  170.                         begin
  171.                            offset := offset + 1;
  172.                            if not varfound then varfound := locatevariable;
  173.                            offset := offset - 1;
  174.                            if grdebug=2
  175.                               then
  176.                                writeln
  177.                         end
  178.                end
  179.             else  { not a null }
  180.                begin
  181.                   if grdebug=2
  182.                      then
  183.                         write(chr(Mem[Segment:offset]));
  184.                end;
  185.          offset := offset + 1;
  186.    end;  { end while loop }
  187. End;  { ReadEnvironment }
  188.  
  189. Procedure StorePath;
  190. { search for each directory delimited by a ';', store it in an array
  191.   and filter non-allowed characters }
  192.  
  193. var
  194.    dirndx : maxdirtype;   { directory counter }
  195.    pc  : 0..MaxDirLength; { when searching: character-in-path counter }
  196.    offsetvarctr : integer; { counter into the the variable's list of dirs }
  197.  
  198. Begin
  199.    pc  := 0;
  200.    dircount := 1;
  201.    offsetvarctr := offsetvarstart;
  202.    While offsetvarctr< offsetvarstart+MaxVarLength do
  203.       begin
  204.          if Mem[Segment:offsetvarctr]=0
  205.             then
  206.                 { null found, so search is complete force exit from loop }
  207.                offsetvarctr := offsetvarstart+MaxVarLength + 1
  208.             else
  209.                if Mem[Segment:offsetvarctr] in
  210.                   { are they allowable chars?  }
  211.                   [33..41,                  { punctuation }
  212.                   44..59,  61,      { punctuation, numbers, ';' }
  213.                   64..90, 92,       { uppercase alphabetics, '\' }
  214.                   97..122]          { lowercase alphabetics }
  215.                     then
  216.                      if Mem[Segment:offsetvarctr]=59
  217.                         then   { the PATH delim }
  218.                            begin
  219.                               { end of one subdirectory, so
  220.                               reset char count, increment dircount }
  221.                               dircount := dircount + 1;
  222.                               pc := 0;
  223.                               if dircount >= MaxDirectories
  224.                                then
  225.                                 begin
  226.                                  if (grdebug=1) or (grdebug=2)
  227.                                   then
  228.                                    writeln(
  229.                                  'Too many Paths encountered... exiting'
  230.                                    );
  231.                                  Halt(1)
  232.                                  { to DOS with ErrorLevel set to 1 }
  233.                                 end;
  234.                            end
  235.                         else
  236.                            begin
  237.                               { save the path character in an array }
  238.                               pc := pc+1;
  239.                               dirlist[dircount][pc] := Mem[Segment:
  240.                                                        offsetvarctr];
  241.                            end;
  242.          offsetvarctr := offsetvarctr + 1;
  243.       end;
  244. end; { StorePath }
  245.  
  246. Procedure ListPath;
  247. { display each directory in the path }
  248.  
  249. var
  250.    dirndx : maxdirtype;   { directory counter }
  251.    pc : integer;        { count characters in the array }
  252.  
  253. begin
  254.    writeln;
  255.    writeln('Number of directories: ', dircount,
  256.             '.  The list of directories:');
  257.    If dircount >= 1
  258.       then
  259.          For dirndx:=1 to dircount do
  260.             begin
  261.                pc := 1;
  262.                While (pc < MaxDirLength) and
  263.                  ( dirlist[dirndx][pc]<>0) do
  264.                   begin
  265.                      { it is a printable char }
  266.                      write(chr(dirlist[dirndx][pc]));
  267.                      pc := pc + 1
  268.                   end;
  269.                   writeln; { a new line }
  270.             end { dirndx loop }
  271.          else
  272.                   writeln('No PATH variable in the environment');
  273. end;  { ListPath }
  274.  
  275. Begin {searchenvironment}
  276.    {segment where the environment starts }
  277.    Segment := MemW[PrefixSeg:$2C];
  278.    ReadEnvironment;
  279.    if varfound
  280.       then
  281.          begin
  282.             StorePath;
  283.             if grdebug=2
  284.                then
  285.                ListPath;
  286.          end
  287.       else
  288.          if (grdebug=1) or (grdebug=2)
  289.             then
  290.                writeln ('Environment variable: ', environvar,' not found');
  291. End; {searchenvironment}
  292.  
  293. procedure BgiFind; { search for the given bgifile }
  294.  
  295. label
  296.    1000;
  297.  
  298. var
  299.    listdirbyte : bytepatharray;
  300.    maxdirs, countdirs : maxdirtype;
  301.    countbyte : integer;
  302.    filerecord : searchrec;
  303.  
  304. begin { bgifind }
  305. { first search for bgi file in the default directory.
  306.   if not found, continue the search by sequentially testing each
  307.   directory in array listdirbyte }
  308.    findfirst ( bgifile+'.bgi', anyfile, filerecord);
  309.    if doserror = 0
  310.       then
  311.          begin
  312.             if grdebug=2
  313.                then
  314.                   writeln ( 'Found in default directory: ', bgifile +
  315.                            '.bgi');
  316.             exit;
  317.          end
  318.       else
  319.          begin
  320.          { initialize the array before using it }
  321.             for maxdirs := 1 to MaxDirectories do
  322.             for countbyte := 1 to MaxDirLength do
  323.                listdirbyte[maxdirs,countbyte] := 0;
  324.             searchenvironment (maxdirs, listdirbyte);
  325.             { convert the byte array into an input for findfirst }
  326.             if varfound
  327.                then
  328.                   for countdirs := 1 to maxdirs do
  329.                      begin
  330.                         dirstring := '';
  331.                         for countbyte := 1 to MaxDirLength do
  332.                            begin
  333.                { starting with the left end of the byte array, stuff the
  334.                character equivalent into the string variable dirstring
  335.                until the first null is reached.  At that byte, substitute
  336.                a '\' if the last character wasn't already a '\' }
  337.                               if listdirbyte[countdirs, countbyte] <> 0
  338.                                then
  339.                                 dirstring := dirstring +
  340.                                   chr (listdirbyte[countdirs, countbyte])
  341.                                else {null byte: end of directory path }
  342.                                 if copy (dirstring, length (
  343.                                    dirstring), 1)<>'\'
  344.                                  then
  345.                                   begin
  346.                                    dirstring := dirstring + '\';
  347.                                    goto 1000;
  348.                                   end;
  349.                            end;
  350.                         1000: findfirst ( dirstring+bgifile+'.bgi', 
  351.                                             anyfile, filerecord);
  352.                         if doserror = 0
  353.                            then
  354.                               begin
  355.                                if grdebug=2
  356.                                 then
  357.                                  writeln ( 'Found: ', dirstring+
  358.                                           bgifile+'.bgi');
  359.                                exit;
  360.                               end
  361.                            else
  362.                               if grdebug=2
  363.                                then
  364.                                 writeln ( 'Did not find: ',
  365.                                          dirstring+bgifile+'.bgi',
  366.                                          ' Dos error: ', doserror );
  367.                      end;
  368.          end; { look in default directory }
  369.  end; { bgifind }
  370.  
  371. begin {main procedure}
  372.    GraphicsDetermine;
  373.    bgifind;
  374.    if varfound
  375.    then
  376.    begin
  377.    { wait to allow observing the screen }
  378.    if grdebug=2
  379.       then
  380.          begin
  381.             writeln('Press <return>');
  382.             readln
  383.          end;
  384.    InitGraph (Graphdriver, Graphmode, dirstring);
  385.    graphstatus := GraphResult;
  386.    if graphstatus <> grOk
  387.       then
  388.          begin
  389.             if (grdebug=1) or (grdebug=2)
  390.                then
  391.                writeln( 'InitGraph error: ', GraphErrorMsg(GraphDriver));
  392.                grsearch := false;
  393.          end
  394.       else
  395.          grsearch := true;
  396.    end
  397.    else
  398.       grsearch := false;
  399.  
  400. end; {main procedure}
  401. begin
  402.    {initialization}
  403. end.
  404.